package com.usian.controller;

import com.usian.common.R;
import com.usian.pojo.Emp;
import com.usian.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/login")
public class LoginController {
    @Autowired
    private EmpService empService;

    /**
     * 登录
     * @param user
     * @param session
     * @return
     */
    @PostMapping("/login")
    public R login(@RequestBody Emp user, HttpSession session){
        R<Emp> login = empService.login(user);
        //判断登陆是否成功
        if (login.getCode()==1){
            session.setAttribute("login",login.getData().getId());
        }
        if (login.getCode()==0){
            return R.error(login.getMsg());
        }

        return R.success(login.getData().getGrade());
    }

    /**
     * 退出
     * @param session
     * @return
     */
    @PostMapping("/logout")
    public R logout(HttpSession session){
        session.invalidate();               //让session失效
        //session.removeAttribute("login"); //清空session
        return R.success("退出登录");
    }

}











